jBGallery.OnLoadImage = function(el, engine, options) {

  //PRIVATE METHODS
  function _onload(ev) {
    engine.loading.hide();
    engine.target.hide().css("visibility","").fadeIn(options.timers.fade);//opera 11 fadeOut/visibility:hidden
  }
  /*
  function _tofix() {
    return options.ie6 ||
    options.webkit ||
    options.style !== "zoom";
  }
  */
  //MAIN
  //if (_tofix()) {
  //4.0
  new jBGallery.Fix(el, engine.target, options);
  //}
  //events
  if (options.clickable) {
    engine.target
    .click( function() {
      engine.right();
      return false;
    })
    .css({
      cursor : "pointer"
    });
  }

  engine.target
  .one("load", function() {
    $('html').removeClass("loading");
  })
  .bind("load", function(ev) {
    _onload(ev);
    options.load(ev);
    options.after(ev);

    if(engine.slideshow()) {
      engine.timeout(ev);
    }
  });
};
jBGallery.Fix = function(el, target, options) {

  //PRIVATE METHODS:
  function _get_dimensions(img) {
    var _cnt = options.fullscreen ? 'body' : el;
    var _bw = $(_cnt).width();
    var _bh = $(_cnt).height();
    var _pm = _bw / _bh;
    var _p = img.width / img.height;
    var _ret = {
      bw : _bw,
      bh : _bh,
      pm : _pm,
      p : _p,
      h : img.height,
      w : img.width
    };
    _img = null;
    return _ret;
  }

  function _fix() {
    
    //4.0 - TODO : refactory
    _fix_containers();
    
    var _img = target.get(0);
    var _f = options.style == 'zoom' ? _fix_zoom : _fit_to_box;
    if (options.ie6 || options.ie7) {
      setTimeout( function() {
        _f(_img)
      }, 10);
    } else {
      _f(_img);
    }
  }

  function _fit_to_box(img) {
    $(img).width("auto").height("auto");
    var _dim = _get_dimensions(img), _force;
    if (options.style == 'original' && _dim.bw >= _dim.w && _dim.bh >= _dim.h)
      return false;
    if(_dim.bw < _dim.bh) {//portrait
      var _ih = _dim.h * _dim.bw / _dim.w;
      if(_force = _ih > _dim.bh) {
        var _iw = _dim.w * _dim.bh / _dim.h;
        _width(_iw, _dim, _force);//original 200x2000
      } else {
        _width(_dim.bw, _dim);
      }
    } else {//landscape
      var _iw = _dim.w * _dim.bh / _dim.h;
      if(_force = _iw > _dim.bw) {
        var _ih = _dim.h * _dim.bw / _dim.w;
        _heigth(_ih, _dim, _force);//original 2000x200
      } else {
        _heigth(_dim.bh, _dim);
      }
    }
  }

  function _fix_zoom(img) {
    $(img).width("auto").height("auto");
    var _dim = _get_dimensions(img);
    if (_dim.bh > _dim.bw) {//portrait
      var _iw = _dim.w * _dim.bh / _dim.h;
      if(_iw < _dim.bw) {
        var _ih = _dim.h * _dim.bw / _dim.w;
        _heigth(_ih, _dim, true);//original 2000x200
      } else {
        _heigth(_dim.bh, _dim, true);
      }
    } else {//landscape
      var _ih = _dim.h * _dim.bw / _dim.w;
      if(_ih < _dim.bh) {
        var _iw = _dim.w * _dim.bh / _dim.h;
        _width(_iw, _dim, true);
      } else {
        _width(_dim.bw, _dim, true);
      }
    }
  }

  function _heigth(nh, dim, force) {
    if(force || options.style == 'centered' || dim.bh < dim.h)
      target.height(nh);
  }

  function _width(nw, dim, force) {
    if(force || options.style == 'centered' || dim.bw < dim.w)
      target.width(nw);
  }


  //containers - 4.0 - TODO refactory
  function _fix_containers(){
    var _cnt = options.fullscreen ? 'body' : el;
    var _bw = $(_cnt).width();
    var _bh = $(_cnt).height();
    
    //TODO: layers
    var _ch = _bh;
    //TODO : test | cache
    $(el).css({
      "height":_bh,
      "width":_bw,
      "line-height":_ch + 'px'//vertical align image
    });
    $('.jbg4-fade, .jbg4-slider', el).height(_ch);
    $('.jbg4-img', el).css({
      "width":_bw
    });
  }
  //MAIN
  //events

  if (options.fullscreen){//event only in fullscreen
    jBGallery.EventTimer(window, ["resize.jbgallery"], options.delays.resize, _fix);
  }

  
  target.bind('load', function(ev) {
    _fix();
  });
};